import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// 创建一个仓库
const store = new Vuex.Store({
  state: {
    num: 10
  },
  actions: {
    /* add_num_async (context, n) {
      console.log(2)
      // context就是store这个实例对象
      setTimeout(() => {
        context.commit('add_num', n)
      }, 2000)
    } */
    add_num_async ({ commit }, n) {
      console.log(2)
      // context就是store这个实例对象
      setTimeout(() => {
        commit('add_num', n)
      }, 2000)
    }
  },
  mutations: {
    add_num (state, n) {
      console.log(3)
      state.num += n
    },
    reduce_num (state) {
      state.num -= 1
    }
  }
})
export default store
